package com.joplus.housekeeper.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ccm on 2016/11/16 16:46
 */

public class WeekUtils {
    //返回星期几
    //放入2010-1-1
    public static String getWeek(String week) {
        Calendar calendar = Calendar.getInstance();
        String[] split = week.split("/");
        calendar.set(Integer.valueOf(split[0]), Integer.valueOf(split[1]) - 1, Integer.valueOf(split[2]));
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            return "星期一";

        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            return "星期二";

        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {

            return "星期三";
        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            return "星期四";
        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            return "星期五";
        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            return "星期六";
        } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return "星期日";
        }
        return null;
    }

    public static boolean compareDay(String time) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String timeT = format.format(date);
        String s = time.split(" ")[0];
        int flag = s.compareTo(timeT);

        if (flag >= 0) {

            return true;
        }
        return false;
    }

    //判断是今天还是明天 或者就是null
    public static String getTt(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String today = format.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        Date t = calendar.getTime();
        String tomorrow = format.format(t);

        if (today.equals(time)) {
            return "今天";
        } else if (tomorrow.equals(time)) {
            return "明天";
        }
        return "";
    }


    //week是0-6
    //例子:时间段的周一周二
    public static List<String> getWeekDay(String from, String to, List<Integer> week) {
//日期开关
        boolean[] open = new boolean[]{false, false, false, false, false, false, false};
        for (int i = 0; i < week.size(); i++) {
            open[week.get(i)] = true;
        }


        Calendar calendar = Calendar.getInstance();
        String[] array = {from, to};
        Date[] ds = new Date[array.length];
        for (int i = 0; i < array.length; i++) {
            String[] fs = array[i].split("-");
            calendar.set(Integer.parseInt(fs[0]), Integer.parseInt(fs[1]) - 1, Integer.parseInt(fs[2]));
            ds[i] = calendar.getTime();
        }
        List day = new ArrayList();
        for (Date x = ds[0]; x.compareTo(ds[1]) < 0; ) {
            calendar.setTime(x);
            calendar.add(Calendar.DATE, 1);
            x = calendar.getTime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(x);
            String today = sdf.format(new Date());


            if (today.compareTo(format)>-1) {
                continue;
            }
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY && open[0]) {

                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY && open[1]) {

                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY && open[2]) {

                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY && open[3]) {
                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY && open[4]) {
                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && open[5]) {
                day.add(format);
            } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY && open[6]) {
                day.add(format);
            }
        }
        return day;


    }
}
